import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import user from "mock/user";

//配置路由
// const routes: Array<RouteRecordRaw> = [
//     {
//         path: "/",
//         name: "home",
//         component: () => import("@/views/home/index.vue"),
//         meta: {
//             title: "首页",
//         },
//         children: [],
//     },
// ]
export const aboutRouter = {
    path: '/about',
    name: 'about',
    component: () => import('@/views/about/index.vue'),
    meta: {},
    children: [],
} as RouteRecordRaw;

const modules: Record<string, any> = import.meta.glob(['./modules/*.ts'], {
    eager: true
});

// 配置路由
const routes: Array<RouteRecordRaw> = [];
Object.keys(modules).forEach((key) => {
    const module = modules[key].default;
    routes.push(module);
});
routes.push(aboutRouter);
//创建路由
const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

const whitelist = ['/login', '/about'];
//路由守卫
router.beforeEach(async (_to, _from, next) => {
    NProgress.start();
    const token = sessionStorage.getItem('userInfo');
    const userIsLogin = token ? true : false;
    if (userIsLogin || whitelist.includes(_to.path)) {
        next();
    } else {
        next('/login');
    }
})
//路由后置守卫
router.afterEach((_to) => {
    NProgress.done();
})
export default router
